package com.army.stock.web;

import com.alibaba.fastjson.JSON;
import com.army.stock.domain.ArmySupplier;
import com.army.stock.service.ArmySupplierService;
import com.army.stock.util.CommonRegExp;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.web.url.BaseUrl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 14/12/25.
 * 军队供应商信息 控制器
 */
@Controller
public class ArmySupplierController {

    private static Logger logger = LoggerFactory.getLogger(ArmySupplierController.class);

    private static String NEW_PAGE = "armysupplier/new";

    private static String UPDATE_PAGE = "armysupplier/edit";

    private static String SUPPLIERS_PAGE = "armysupplier/suppliers";

    @Autowired
    private ArmySupplierService armySupplierService;

    @RequestMapping(value = "/suppliers", method = RequestMethod.GET)
    public String suppliers(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        PaginatedArrayList<ArmySupplier> pageInfo = armySupplierService.getArmySupplierPageInfo(
                baseUrl.getCurrentPage(), 10, baseUrl.getQueryMap());
        //data   2014-12-27 00:00:00
        //TIME   1970-01-01 15:30:12
        //TIMESTAMP 2014-12-27 15:30:12
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return SUPPLIERS_PAGE;
    }

    @RequestMapping(value = "/suppliers/new", method = RequestMethod.GET)
    public String addsupplierPage() {
        return NEW_PAGE;
    }

    @RequestMapping(value = "/suppliers/add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, ModelMap modelMap, ArmySupplier armySupplier) {
        if (StringUtils.isBlank(armySupplier.getSupplierCode())) {
            modelMap.put("errorMsg", "供应商编码不能为空");
            return NEW_PAGE;
        }
        if (!armySupplier.getSupplierCode().matches(CommonRegExp.CHARACTER_NUMBER)) {
            modelMap.put("errorMsg", "供应商编码只能为英文中文");
            return NEW_PAGE;
        }
        if (StringUtils.isBlank(armySupplier.getSupplierName())) {
            modelMap.put("errorMsg", "供应商名称不能为空");
            return NEW_PAGE;
        }
        //设置操作人
        armySupplier.setOperator("admin");
        armySupplierService.insertArmySupplier(armySupplier);
        return "redirect:/suppliers";
    }

    @RequestMapping(value = "/suppliers/{supplierId}/update", method = RequestMethod.GET)
    public String editPage(@PathVariable(value = "supplierId") String supplierId, HttpServletRequest request, ModelMap modelMap) {
        logger.info("转向修改供应商页面supplierId:" + supplierId);
        modelMap.put("supplierId", supplierId);
        ArmySupplier armySupplier = armySupplierService.getArmySupplierById(Long.valueOf(supplierId));
        modelMap.put("armySupplier", armySupplier);
        return UPDATE_PAGE;
    }

    @RequestMapping(value = "/suppliers/update", method = RequestMethod.POST)
    public String update(HttpServletRequest request, ModelMap modelMap, ArmySupplier armySupplier) {
        if (StringUtils.isBlank(armySupplier.getSupplierCode())) {
            modelMap.put("errorMsg", "供应商编码不能为空");
            return NEW_PAGE;
        }
        if (StringUtils.isBlank(armySupplier.getSupplierName())) {
            modelMap.put("errorMsg", "供应商名称不能为空");
            return NEW_PAGE;
        }
        armySupplierService.updateArmySupplier(armySupplier);
        return "redirect:/suppliers";
    }

    @RequestMapping(value = "/suppliers/del", method = RequestMethod.POST)
    @ResponseBody
    public String del(HttpServletRequest request, ModelMap modelMap) {
        String id = request.getParameter("id");
        logger.info("尝试删除供应商编号:" + id);
        Integer affectedRows = armySupplierService.delArmySupplier(Long.valueOf(id));
        return String.valueOf(affectedRows);
    }

}
